// pages/idAddress/idAddress.js
let util = require('../../utils/util');
const api = require('../../utils/api');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
    startX: 0, //触摸开始的X轴位置
    delBtnWidth: 100, //删除按钮宽度单位（rpx）
    delStyle: "",
    isEmpty: false, //地址是否为空
    // moren:false
  },
  //选中地址跳转到订单确认页面
  onSelectAddress(e) {
    let curIdx = e.currentTarget.dataset.index;
    let id = this.data.addressList[curIdx].id;
    app.global.address = this.data.addressList[curIdx];
    wx.navigateTo({
      url: '/pages/confirmation/confirmation?id=' + id,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //添加地址
  toEditAddress() {
    wx.navigateTo({
      url: '/pages/editAddress/editAddress',
    })
  },
  // 开始触发滑动
  touchS(e) {
    if (e.touches.length == 1) {
      this.setData({
        //记录触摸起始位置的X坐标
        startX: e.touches[0].clientX
      });
    }
  },
  // 触摸过程
  touchM: function (e) {
    var that = this
    if (e.touches.length == 1) {
      //记录触摸点位置的X坐标
      var moveX = e.touches[0].clientX;
      //计算手指起始点的X坐标与当前触摸点的X坐标的差值
      var disX = that.data.startX - moveX;
      //delBtnWidth 为右侧按钮区域的宽度
      var delBtnWidth = that.data.delBtnWidth;
      var delDisX = delBtnWidth - disX;
      var txtStyle = "";
      if (disX == 0 || disX < 0) { //如果移动距离小于等于0，文本层位置不变
        txtStyle = "left:0px";
      } else if (disX > 0) { //移动距离大于0，文本层left值等于手指移动距离
        txtStyle = "left:-" + disX + "rpx";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-" + delBtnWidth + "rpx";
        }
      }
      //获取手指触摸的是哪一个item
      var index = e.currentTarget.dataset.index;
      var list = that.data.addressList;
      //将拼接好的样式设置到当前item中
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        addressList: list
      });
    }
  },
  touchE: function (e) {
    var that = this;
    if (e.changedTouches.length == 1) {
      //手指移动结束后触摸点位置的X坐标
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = that.data.startX - endX;
      var delBtnWidth = that.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = that.data.addressList;
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      that.setData({
        addressList: list
      });
    }
  },
  //删除item  
  delAddress: function(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "确定要删除吗",
      content: "确定要删除该地址吗",
      success(res) {
        if (res.confirm) {
          util.myRequest(api.addressDelete, {
              id: that.data.addressList[id].id
            },
            res => {
              util.myRequest(api.addressList, {},
                res => {
                  that.setData({
                    addressList: res.data.data
                  })
                }
              )
            },
            "POST"
          )
        }
      }
    })
  },
  getInfo: function() {
    let that = this;
    util.myRequest(api.addressList, {},
      res => {
        // that.setData({
        //   addressList: res.data.data
        // })
        for (let i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].is_default == 1){
            res.data.data[i]['moren'] = true
            
          }
          else{
            res.data.data[i]['moren'] = false
          }
         
        }
        that.setData({
          addressList: res.data.data
        })
        console.log(res.data.data)
        if (that.data.addressList.length == 0) {
          that.setData({
            isEmpty: true
          })
        }
      }
    )
  },
  onLoad() {
    console.log(Page.prototype.route);
    this.getInfo()
  },
  onShow() {
    this.getInfo()
  },

})